package com.atheima.reggie.controller;

import com.atheima.reggie.common.R;
import com.atheima.reggie.common.ValidateUtil;
import com.atheima.reggie.dto.SetmealDto;
import com.atheima.reggie.entity.Category;
import com.atheima.reggie.entity.Setmeal;
import com.atheima.reggie.service.CategoryService;
import com.atheima.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealDishController {

    @Autowired
    private SetmealService service;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R save(@RequestBody SetmealDto setmealDto) {
        service.saveWithMeal(setmealDto);
        return R.ok();
    }



    @GetMapping("/page")
    public R page(int page,int pageSize,String name) {
        Page<Setmeal> employeePage = new Page<>(page, pageSize);

        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        //wrapper.orderByAsc("sort");
        if(ValidateUtil.isNotEmpty(name)) {
            wrapper.like("name",name);
        }

        service.page(employeePage,wrapper);
        return R.ok("成功").put("data",employeePage);
    }

    @GetMapping("/list")
    public R list(Setmeal setmeal) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id",setmeal.getCategoryId());
        List<Setmeal> list = service.list(wrapper);
        return R.ok().put("data",list);
    }


}
